public with sharing class ctlr_AccountLocation 
{

	//For Variables...
    Public List<AccountWrapper> AccountWrapperList {get; set;}
    public Account_Location__c accountLocationObject {get; set;}
    public Boolean actionbuttonADD {get; set;}
    public Boolean actionbuttonEdit {get; set;}
    public string RowIndex {get; set;}
    public string RowIndex2 {get; set;}
    public string accountID {get; set;}
    public string accountName {get;set;}
    public Boolean refreshPage {get;set;}
    Public Boolean delButtonFlag {get;set;}
    Public Boolean addButtonFlag {get;set;}
    Public Boolean editButtonFlag {get;set;}
    
    //For Contructors...
    public ctlr_AccountLocation(ApexPages.StandardController controller) 
    {
         refreshPage = false;
         
         //Flag To Control the Trigger...
         delButtonFlag = true;
         addButtonFlag = true;
         actionbuttonADD = true;
         editButtonFlag = true;
    }
    
    //For Generating the Account Locations Initiator Method....
    public PageReference generateAccountLocationModule()
    {
        List<Account> accountList = [select id,RecordTypeId,Number_Of_Tenancy_Contract_Renwal__c,Name,License_Application_Step__c,Residence_Visa_Quota_Left_2__c,Supended_Approved_suspentions_No__c, Number_Of_Approved_Suspension_Record__c, Login_User_Profile__c FROM Account WHERE Id = :ApexPages.CurrentPage().getParameters().get('id')];
        accountLocationObject = new Account_Location__c();
        
        AccountWrapperList = new List<AccountWrapper>();
        
        For(Account accountList_trav : accountList)
        {
            accountName = accountList_trav.name;
            accountLocationObject.Account__c = accountList_trav.id;
            accountID = accountList_trav.Id;
            //AccountWrapperList.Add(new AccountWrapper(accountList_trav.name, accountList_trav.Receipt_Type__c, accountList_trav.id));
            AccountWrapperList.Add(new AccountWrapper(accountList_trav.name, accountList_trav.License_Application_Step__c, accountList_trav.Residence_Visa_Quota_Left_2__c, accountList_trav.id));
        }
        
        for(AccountWrapper AccountWrapper_trav : AccountWrapperList)
        {
            try
            {
                AccountWrapper_trav.accountLocationList = [SELECT id,renewAddLocationFlag__c,isRenewed__c,Previous_Location__r.Name,New_Location__r.Name, Previous_Tenancy_Contract__r.Name,Flag__c,name,New_Location__c,Previous_Tenancy_Contract__c,Previous_Location__c,Account__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c FROM Account_Location__c where Account__c = :AccountWrapper_trav.RecordId AND Delete_Flag__c =: false AND isRenewed__c =: false AND renewAddLocationFlag__c =: false];
                System.debug('The Total Number of Amendment Activities in Wrapper class is => '+ AccountWrapper_trav.accountLocationList.size());       
            }
            catch(System.Exception er)
            {
                AccountWrapper_trav.accountLocationList = new List<Account_Location__c>();
            }
        }
        
        
        CreatedDate = System.Today().format();
        actionbuttonADD = true;
        actionbuttonEdit = false;
        
        return null;   
    }
   
    public string CreatedDate {get; set;}
    
    Public Boolean InitializerFlag = true;
    //System.debug('The InitializerFlag Value Before => '+ InitializerFlag );
    public string check1 {get; set;}
    public PageReference additems()
    {
        check1 = 'entered';
        accountLocationObject.Account__c = ApexPages.CurrentPage().getParameters().get('id');
        
        try
        {
            Decimal BalanceCheck = 0.0;
            for(AccountWrapper AccountWrapper_trav : AccountWrapperList)
            {
                System.debug('The Receipt Wrapper Id => '+ AccountWrapper_trav.RecordID);
                check1 = AccountWrapper_trav.RecordID;
                System.Debug(AccountWrapper_trav.RecordID + ' - ' + accountLocationObject.Account__c);
                check1 = AccountWrapper_trav.RecordID + '-' + accountLocationObject.Account__c;
                if(AccountWrapper_trav.RecordID == accountLocationObject.Account__c)
                {
                    if(AccountWrapper_trav.accountLocationList.size() < 12)
                    {
                        System.Debug('Before Insert the Payment');
                        accountLocationObject.Add_Flag__c = true;
                        //accountLocationObject.Active__c = true;
                        insert(accountLocationObject);
                        
                        
                        //For Location Needs to be Booked...
                        Location__c locationObject = null;
                        locationObject = [SELECT Id,Location_Booked_Information_Flag__c FROM Location__c where Id = : accountLocationObject.New_Location__c];
                        
                        if(locationObject != null)
                        {
                        	locationObject.Location_Booked_Information_Flag__c = true;
                        	
                        	update locationObject;
                        }
                        
                        check1 = accountLocationObject.Id;
                        accountLocationObject = new Account_Location__c();
                        
                        //For Page Refreshing...
                        refreshPage = true;
                    }
                    else
                    {
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'More than twelve(12) Shareholderes are not allowed for Amendment #: ' + AccountWrapper_trav.ReceiptNumber + '. Please adjust the balance amount in existing payments.');
                        ApexPages.addMessage(msg);
                        break;
                    }
                }
            }
            for(AccountWrapper AccountWrapper_trav : AccountWrapperList)
            {
                try
                {
                    AccountWrapper_trav.accountLocationList = [SELECT id,renewAddLocationFlag__c,isRenewed__c,Previous_Location__r.Name,New_Location__r.Name,Previous_Tenancy_Contract__r.Name,Flag__c,New_Location__c,Previous_Tenancy_Contract__c,Previous_Location__c,name,Account__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c FROM Account_Location__c where Account__c = :AccountWrapper_trav.RecordId  AND Delete_Flag__c =: false AND isRenewed__c =: false AND renewAddLocationFlag__c =: false];
                    
                }
                catch(System.Exception er)
                {
                    AccountWrapper_trav.accountLocationList = new List<Account_Location__c>();
                }
            }
            return null;
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage() + ' - Line Number: ' + er.getlinenumber());
            ApexPages.addMessage(msg);
            return null;
        }
    }
    
    public void EditAllow()
    {
        System.debug('The RowIndex Value => '+ RowIndex);
        System.debug('The Last Index Of ":" => '+ RowIndex.lastIndexof(':'));
        System.debug('The Last Index Of "J" => '+ RowIndex.lastIndexof('j'));
        string valindex = RowIndex.substring(RowIndex.lastIndexof(':') - 1, RowIndex.lastIndexof(':'));
        string val2index = RowIndex.substring(RowIndex.lastIndexof('j') - 2, RowIndex.lastIndexof('j') - 1);
        RowIndex = valindex;
        
        System.debug('The val1Index => '+ valindex);
        System.debug('The val2Index => j_id0:MainForm:j_id50:0:j_id52:0:EDITID'+ val2index);
        
        integer indexA = integer.valueof(valindex);
        integer indexB = integer.valueof(val2index);
        
        System.debug('The Receipt Wrapper Payment List => '+ AccountWrapperList[indexB].accountLocationList.size() + ' && The Index A =>'+ indexA + ' && The Index B => '+ indexB);
        accountLocationObject = new Account_Location__c();
        accountLocationObject = AccountWrapperList[indexB].accountLocationList.get(indexA);
        System.debug('The New Payment with wrapper Class');
        actionbuttonADD = false;
        actionbuttonEdit = true;
    }
    
    public void DelAllow()
    {
        string valindex = RowIndex.substring(RowIndex.lastIndexof(':') - 1, RowIndex.lastIndexof(':'));
        
        System.debug('The Val Index 1 => '+ valindex );
        System.debug('The Last Index Of ":" => '+ RowIndex.lastIndexof(':'));
        System.debug('The Last Index Of "J" => '+ RowIndex.lastIndexof('j'));
        
        string val2index = RowIndex.substring(RowIndex.lastIndexof('j') - 2, RowIndex.lastIndexof('j') - 1);
        System.debug('The Val Index 2 => '+ val2index );
        
        
        RowIndex = valindex;
        
        integer indexA = integer.valueof(valindex);
        integer indexB = integer.valueof(val2index);
        Account_Location__c PT = AccountWrapperList[indexB].accountLocationList.get(indexA);
        
        try
        {
            PT.Edit_Flag__c = false;
            PT.Delete_Flag__c = true;
            System.debug('Before Update Delete Flag => ' + PT.Delete_Flag__c);
            update(PT);
            //For Page Refreshing...
            refreshPage = true;
                
            System.debug('After Update Delete Flag => ' + PT.Delete_Flag__c);
            //delete(PT);
            accountLocationObject = new Account_Location__c();
            for(AccountWrapper AccountWrapper_trav : AccountWrapperList)
            {
                try
                {
                    AccountWrapper_trav.accountLocationList = [SELECT id,renewAddLocationFlag__c,isRenewed__c,Previous_Location__r.Name,New_Location__r.Name,Previous_Tenancy_Contract__r.Name,Flag__c,New_Location__c,Previous_Tenancy_Contract__c,Previous_Location__c,name,Account__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c FROM Account_Location__c where Account__c = :AccountWrapper_trav.RecordId AND Delete_Flag__c =: false AND isRenewed__c =: false AND renewAddLocationFlag__c =: false]; 
                    
                }
                catch(System.Exception er)
                {
                    AccountWrapper_trav.accountLocationList = new List<Account_Location__c>();
                }
            }
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage());
            ApexPages.addMessage(msg);
        }
        
        actionbuttonADD = true;
        actionbuttonEdit = false;
    }
    
    public string updateitemlist()
    {
        try
        {
            for(AccountWrapper AccountWrapper_trav : AccountWrapperList)
            {
                check1 = AccountWrapper_trav.RecordID;
                System.Debug(AccountWrapper_trav.RecordID + ' - ' + accountLocationObject.Account__c);
                Decimal BalanceCheck = 0;
                if(AccountWrapper_trav.RecordID == accountLocationObject.Account__c)
                {
	                accountLocationObject.Edit_Flag__c = true;
	                accountLocationObject.Delete_Flag__c = false;
	                update(accountLocationObject);
	                
	                
	                //For Location Needs to be Booked...
                    Location__c locationObject = null;
                    locationObject = [SELECT Id,Location_Booked_Information_Flag__c FROM Location__c where Id = : accountLocationObject.New_Location__c];
                    
                    if(locationObject != null)
                    {
                    	if(locationObject.Location_Booked_Information_Flag__c != true)
                    	{
                    		locationObject.Location_Booked_Information_Flag__c = true;
	                    	
	                    	//Update the Location Object...
	                    	update locationObject;	
                    	}
                    	
                    }
	                
	                
	                //For Page Refreshing...
	                refreshPage = true;
	                for(AccountWrapper AccountWrapper_trav1 : AccountWrapperList)
	                {
	                    try
	                    {
	                        AccountWrapper_trav.accountLocationList = [SELECT id,renewAddLocationFlag__c,isRenewed__c,Previous_Location__r.Name,New_Location__r.Name,Previous_Tenancy_Contract__r.Name,Flag__c,name,New_Location__c,Previous_Tenancy_Contract__c,Previous_Location__c,Account__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c FROM Account_Location__c where Account__c = :AccountWrapper_trav.RecordId AND Delete_Flag__c =: false AND isRenewed__c =: false AND renewAddLocationFlag__c =: false];
	                       
	                        
	                    }
	                    catch(System.Exception er)
	                    {
	                        AccountWrapper_trav1.accountLocationList = new List<Account_Location__c>();
	                    }
	                }
	                
	                accountLocationObject = new Account_Location__c();
	                actionbuttonADD = true;
	                actionbuttonEdit = false;
                    
                    break;
                }
            }
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage());
            ApexPages.addMessage(msg);
        }

        return null;
    }
    
    public void cancelEdit()
    {
        try
        {
            accountLocationObject = new Account_Location__c();
            for(AccountWrapper AccountWrapper_trav : AccountWrapperList)
            {
                try
                {
                    AccountWrapper_trav.accountLocationList = [SELECT id,renewAddLocationFlag__c,isRenewed__c,Previous_Location__r.Name,New_Location__r.Name,Previous_Tenancy_Contract__r.Name,Flag__c,name,New_Location__c,Previous_Tenancy_Contract__c,Previous_Location__c,Account__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c FROM Account_Location__c where Account__c = :AccountWrapper_trav.RecordId AND Delete_Flag__c =: false AND isRenewed__c =: false AND renewAddLocationFlag__c =: false];
                }
                catch(System.Exception er)
                {
                    AccountWrapper_trav.accountLocationList = new List<Account_Location__c>();
                }
            }
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage());
            ApexPages.addMessage(msg);
        }
        
        actionbuttonADD = true;
        actionbuttonEdit = false;
    }
    

    //Receipt Wrapper Class..
    class AccountWrapper
    {
        Public String ReceiptNumber {get; set;}
        Public String ReceiptGroup {get; set;}
        Public Decimal ReceiptAmount {get; set;}
        Public Decimal ReceiptBalance {get; set;}
        Public ID RecordId {get; set;}
        
        Public List<Account_Location__c> accountLocationList {get; set;}
        
        public AccountWrapper(String RNumber, String RGroup,Decimal RAmount, ID RID)
        {
            ReceiptNumber = RNumber;
            ReceiptGroup = RGroup;
            ReceiptAmount = RAmount;
            ReceiptBalance = RAmount;
            RecordId = RID;
        }
        
	}
	
}